#ifndef GAMESCENE_H_20120221
#define GAMESCENE_H_20120221


#include "cocos2d.h"
#include "InlineFunc.h"
#include "Archer.h"
#include "NetworkDelegate.h"
#include "CocosUI/CocosUI.h"
#include "Scene.h"
#include <queue>
const int LAYERNUM_MAX = 5;

/*
const int LAYER_BACK    = 0;
const int LAYER_BACKOBJ = 1;
const int LAYER_MIDDLE  = 2;
const int LAYER_MAIN    = 3;
const int LAYER_FRONT   = 4;
 */

/*
enum LayerNum
{
    LAYER_BACK,
    LAYER_BACKOBJ,
    LAYER_MIDDLE,
    LAYER_MAIN,
    LAYER_FRONT,
    LAYER_UI
};
*/
//const int LAYER_MAX     = LAYER_UI+1;




const int MAIN_SCENE_WIDTH = 3096;

const int GROUND_HEIGHT = 148;

const float MAINSCENE_WAIT_MAX = 2.5f;

const int BTN_GAMESCENE_MOVELEFT  = 0;
const int BTN_GAMESCENE_MOVERIGHT = 1;
const int BTN_GAMESCENE_SENDMSG   = 2;

const int CHATLABEL_MAX = 3;

class CGameScene : public cocos2d::CCLayer, public CUIListener, public CFightDelegate, public CGameDelegate
{
public:
    enum GameStep
    {
        GAMESTEP_NONE,
		GAMESTEP_BEGIN,
        GAMESTEP_PREPARE,
        GAMESTEP_ADJUST,
        GAMESTEP_PRESHOOT,
        GAMESTEP_SHOOT,
		GAMESTEP_HURT,
        GAMESTEP_CHANGETURN,
        GAMESTEP_OVER
    };
    enum GameTurn
    {
        GAMETURN_NONE,
        GAMETURN_LEFT,
        GAMETURN_RIGHT
    };
public:
    static void SetInitPactet(const anFight &p);
public:
    CGameScene()
        : m_pArcherTarget(NULL)
        , m_pArcherLocal(NULL)
        , m_curTime(0)
        , m_endTime(0)
        , m_bFirstMoveTouch(true)
        , m_gameStep(GAMESTEP_PREPARE)
        , m_gameTurn(GAMETURN_NONE)
        , m_pArrowFlying(NULL)
        //, m_mainScale(1.0f)
        , m_pMoveTouch(NULL)
        , m_pScaleTouch(NULL)
        , m_gameWaitTimer(0) 
		, m_userScale(1.0f)
		//, m_scaleSpeed(1.0f)
		, m_lastChatLabel(CHATLABEL_MAX-1)
    {
        m_pServer = CServerHelper::GetInstance();
        m_pServer->SetFightDelegate(this);
        m_pServer->SetGameDelegate(this);
    }
    virtual bool init();
	virtual void onEnter();
	virtual void onExit();
	virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	virtual void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
	LAYER_NODE_FUNC(CGameScene);
	LAYER_SHOW_FUNC(CGameScene);

	virtual void keyboardWillShow(cocos2d::CCIMEKeyboardNotificationInfo& info);
	virtual void keyboardWillHide(cocos2d::CCIMEKeyboardNotificationInfo& info);

    // 消息管理
    virtual void onReceiveMsg(int aID, int event = 0, const void *pData = NULL);
    virtual void enemyMove(int id, const Move &moveInfo);
    virtual void attackSuccess(int id, const AttactRecv &attackInfo);
	virtual void roomChat(int id, const char *szRoom, int len);

    virtual void userLost();
	virtual void gameServerRejected();
	virtual void gameServerLost();
    

    // 箭矢管理
    void AddArrow(CArrow *pNewArrow);
    void RemoveTopArrow();
    void CleanArrows();
    
    // 游戏节奏
	void InitUI();
    void GameOver();
    void Restart();
    void OnUpdate(cocos2d::ccTime dt);
	bool UpdateLogicWorld(cocos2d::ccTime curTime);
    void UpdateObjects(cocos2d::ccTime dt);
	void GameBegin(cocos2d::ccTime dt);
	void MoveTable(cocos2d::ccTime dt);
    void PreShoot(cocos2d::ccTime dt);
    void ArcherShoot(cocos2d::ccTime dt);
	void HurtStep(cocos2d::ccTime dt);
    void ArcherAdjust(cocos2d::ccTime dt);
    bool ChangeTurn(cocos2d::ccTime dt);
    
    // 辅助函数
    void InitData();
    void ReleaseData();
    void InitPlayers();
private:
    void EnableButtons(bool bEnable);
private:
    cocos2d::CCLabelTTF *m_pFntLeftHp;
    cocos2d::CCLabelTTF *m_pFntRightHp;

	cocos2d::CCLayer    *m_pLayUI;
	CScene              *m_pCurScene;
    
  
    // 游戏控制
    cocos2d::CCTouch        *m_pMoveTouch;
    cocos2d::CCTouch        *m_pScaleTouch;
    bool                     m_bFirstMoveTouch;
    bool                     m_bFirstScaleTouch;
	
	float                    m_userScale;
    
    // 游戏节奏
    GameStep  m_gameStep;       // 当前游戏环节
    GameTurn  m_gameTurn;       // 当前行动角色
    GameTurn  m_myGameTurn;     // 本地角色行动
	float     m_endTime;
    float     m_curTime;
    float     m_gameWaitTimer;
    
    // 人物相关
    CArcher              *m_pArcherLocal;
	CArcher              *m_pArcherTarget;
    CArcher              *m_pArcherLeft;
	CArcher              *m_pArcherRight;
    CArrow               *m_pArrowFlying;
    std::queue<CArrow *>  m_qArrow;
    
    // 控件
    CCocosButton    *m_pBtnMoveLeft;
    CCocosButton    *m_pBtnMoveRight;
	cocos2d::CCLabelTTF *m_pLabelChat[3];
	std::string          m_strChat;
	cocos2d::CCTextFieldTTF *m_pTFRoomSend;
	CCocosButton    *m_pBtnSend;
	float            m_lastAdjustVertY;
	int              m_lastChatLabel;
    
    // 通讯
    static anFight            s_initData;
    CServerHelper            *m_pServer;
    queue<AttactRecv *>       m_qLocalAttack;
    queue<AttactRecv *>       m_qEnemyAttack;
    queue<Move *>             m_qEnemyMove;
};
#endif